import 'package:distribution_mall_flutter/constant/cf_constant.dart';
import 'package:distribution_mall_flutter/entity/order/detail/order_detail_entity.dart';
import 'package:distribution_mall_flutter/ext/map_ext.dart';
import 'package:distribution_mall_flutter/generated/l10n.dart';
import 'package:distribution_mall_flutter/provider/order/order_detail.dart';
import 'package:distribution_mall_flutter/utils/common_widget.dart';
import 'package:distribution_mall_flutter/utils/utils.dart';
import 'package:distribution_mall_flutter/widgets/card.dart';
import 'package:distribution_mall_flutter/widgets/icon_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class SummaryCard extends ConsumerWidget {
  const SummaryCard({super.key});

  Widget listRender(
      {required String leftText,
      required String rightText,
      bool showDis = false}) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: EdgeConstant.edge5),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            leftText,
            style: const TextStyle(
                color: ColorConstant.grey153, fontSize: FontConstant.f14),
          ),
          Utils.currencyWidget(rightText,
              showDis: showDis,
              symbolStyle: const TextStyle(
                  color: ColorConstant.black51, fontSize: FontConstant.f12),
              commonStyle: const TextStyle(
                  color: ColorConstant.black51,
                  fontWeight: FontWeight.bold,
                  fontSize: FontConstant.f14))
        ],
      ),
    );
  }

  Widget disRender({required List<OrderPromotionList> orderPromotionList}) {
    Widget row(String leftText, double? rightText) => Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              leftText,
              style: const TextStyle(
                  color: ColorConstant.grey153, fontSize: FontConstant.f14),
            ),
            const Spacer(),
            const Text(
              "-",
              style: TextStyle(
                  color: ColorConstant.grey136, fontSize: FontConstant.f13),
            ),
            Utils.currencyWidget(rightText,
                showDis: true,
                crossAxis: CrossAxisAlignment.center,
                symbolStyle: const TextStyle(
                    color: ColorConstant.grey136, fontSize: FontConstant.f10),
                commonStyle: const TextStyle(
                    color: ColorConstant.grey136, fontSize: FontConstant.f13))
          ],
        );
    return Visibility(
        visible: orderPromotionList.isNotEmpty == true,
        child: Container(
          margin: const EdgeInsets.only(bottom: EdgeConstant.edge15),
          padding: const EdgeInsets.symmetric(horizontal: 10),
          decoration: const BoxDecoration(
            color: ColorConstant.grey246,
            borderRadius: BorderRadius.all(Radius.circular(8)),
          ),
          child: Column(
            children: [
              ...orderPromotionList
                  .map<Widget>((e) => Container(
                      margin: const EdgeInsets.symmetric(vertical: 5),
                      child: row(e.promotionCode ?? '', e.discountAmount!)))
                  .superJoin(const Divider(
                    height: 0.5,
                    color: ColorConstant.grey228,
                  )),
            ],
          ),
        ));
  }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    OrderDetailEntity? order =
        ref.watch(orderDetailProvider.select((value) => value.detal));
    Amount? amount = order?.amount;
    List<OrderPromotionList>? orderPromotionList = order?.orderPromotionList;
    return EPCard(
      margin: const EdgeInsets.fromLTRB(
          EdgeConstant.edge10, EdgeConstant.edge10, EdgeConstant.edge10, 0),
      padding: const EdgeInsets.symmetric(
        vertical: EdgeConstant.edge15,
        horizontal: EdgeConstant.edge10,
      ),
      child: Column(
        children: [
          CommonTextWidget.titleText(
            text: S.of(context).order_orderSum,
            padding: const EdgeInsets.only(bottom: EdgeConstant.edge5),
          ),
          listRender(
              leftText: S.of(context).totalProducts,
              rightText: '${amount?.goodsTotalAmount ?? ''}'),
          listRender(
              leftText: S.of(context).shippingFee,
              rightText: '${amount?.shippingAmount ?? ''}'),
          listRender(
            leftText: S.of(context).checkout_promotion_code,
            rightText: '${amount?.totalDiscount ?? ''}',
            showDis: true,
          ),
          disRender(
            orderPromotionList: orderPromotionList ?? [],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Utils.currencyWidget(amount?.totalAmount,
                  label: '${S.current.total}: ',
                  labelStyle: const TextStyle(
                    fontSize: FontConstant.f12,
                    color: Colors.black,
                    letterSpacing: TextConstant.letterSpacing,
                  ),
                  commonStyle: const TextStyle(
                    fontSize: FontConstant.f18,
                    fontWeight: FontWeight.bold,
                    color: Color.fromRGBO(227, 50, 11, 1),
                    letterSpacing: TextConstant.letterSpacing,
                  ))
            ],
          )
        ],
      ),
    );
  }
}
